<template>
  <GcTable
    v-loading="loading"
    v-model:searchKeywords="searchKeywords"
    :pagination="pagination"
    :columns="ARTICLE_COMMENT_COLUMNS"
    :tableData="tableData"
    :showFilter="false"
    searchPlaceholder="请输入昵称/评论内容进行搜索"
    @current-change="handlerCurrentChange"
    @search="handlerSearch">
    <template #operate="{ row }">
      <div class="operate-btn-group center">
        <gc-icon-button
          v-permission="'article-comment-permission:btn:delete'"
          type="link"
          @click="deleteComment(row.id)">删除</gc-icon-button>
      </div>
    </template>
  </GcTable>
</template>

<script setup lang="ts" name="Comment">
import { ref } from 'vue'
import { ARTICLE_COMMENT_COLUMNS } from './const'
import usePagination from '@/hooks/usePagination'
import type { Pagination } from '@/@type'
import { apiGetArticleCommentList, apiDeleteArticleComment } from '@/http/service'
import type { Comment } from '@/http/type'
import { ElMessage, ElMessageBox } from 'element-plus'

interface ResponseArticleComment extends Pagination {
  dataList: Array<Comment>
}
const { pagination, loading } = usePagination()

const tableData = ref<Array<Comment>>([])
const searchKeywords = ref('')

/**
 * 文章评论列表查询
 */
async function findArticleCommentList () {
  const searchParams = {
    pageNo: pagination.pageNo,
    pageSize: pagination.pageSize,
    searchKeywords: searchKeywords.value
  }
  loading.value = true
  try {
    const { dataList, ...pageInfo } = await apiGetArticleCommentList<ResponseArticleComment>(searchParams)
    tableData.value = dataList
    Object.assign(pagination, pageInfo)
  } catch (error) {
    tableData.value = []
  } finally {
    loading.value = false
  }
}
findArticleCommentList()

/**
 * 文章评论删除
 * @param id
 */
async function deleteComment (id: string) {
  await ElMessageBox.confirm(
    '删除该条评论会同时删除该评论下的所有评论，确认删除？',
    '提示',
    { type: 'warning' }
  )
  await apiDeleteArticleComment({ id })
  ElMessage.success('删除评论成功')
  findArticleCommentList()
}

function handlerSearch () {
  pagination.pageNo = 1
  findArticleCommentList()
}

function handlerCurrentChange (val: number) {
  pagination.pageNo = val
  findArticleCommentList()
}
</script>

<style lang="scss" scoped></style>
